var data =  window.parent.App.singleMap;
var map;
var mapVar = {
	data: window.parent.App.singleMap,
	infoWindow: '',
	lng: '113.977457',
	lat: '22.538121',
	time: '',
	marker: '',
	geocoder: '',
	refresh: 30000
}

//在指定位置打开信息窗体
AMap.service('AMap.Geocoder',function(){//回调函数
    //实例化Geocoder
    geocoder = new AMap.Geocoder({
        city: "010"//城市，默认：“全国”
    });
})
map = new AMap.Map("container_goMap", {
    resizeEnable: true,
  	center: [mapVar.lng, mapVar.lat],
    zoom: 14
});
map.setFitView();
mapAjax();
// 循环刷新位置
setInterval(function(){
	mapAjax();
}, mapVar.refresh);
// 获取数据
console.log(App)
function mapAjax(){
	console.log(App)
	App.ajax({
		data: {plate: encodeURI(mapVar.data.no)},
		type: 'GET',
		resUrl: 'vehicle/getLocationByBusNo',
		callback: function(rb){
			console.log(rb)
			if (!rb.status) {
	            layer.msg(rb.msg, { icon: 5 });
	            return;
	       	};
	       	var data = rb.data;
	       	mapVar.lng = data.longitude;
	       	mapVar.lat = data.latitude;
	       	mapVar.time = data.postime;
	       	mapVar.refresh = data.refreshInterval * 1000;
			
			refreshPos()
		}
	});	
}
// 点击刷新位置
function refreshPos() {
    map.panTo([mapVar.lng, mapVar.lat]);
    addMarker();
    openInfo();
}
// marker标记
function addMarker() {
    map.clearMap();
    mapVar.marker = new AMap.Marker({
        map: map,
        position: [mapVar.lng, mapVar.lat]
    });
	//鼠标点击marker弹出自定义的信息窗体
	AMap.event.addListener(mapVar.marker, 'click', function() {
	    infoWindow.open(map, mapVar.marker.getPosition());
	});
}
//关闭信息窗体
function closeInfoWindow() {
    map.clearInfoWindow();
}
//构建自定义信息窗体
function createInfoWindow(title, content) {
    var info = document.createElement("div");
    info.className = "info";

    //可以通过下面的方式修改自定义窗体的宽高
    //info.style.width = "400px";
    // 定义顶部标题
    var top = document.createElement("div");
    var titleD = document.createElement("div");
    var closeX = document.createElement("img");
    top.className = "info-top";
    titleD.innerHTML = title;
    closeX.src = "http://webapi.amap.com/images/close2.gif";
    closeX.onclick = closeInfoWindow;

    top.appendChild(titleD);
    top.appendChild(closeX);
    info.appendChild(top);

    // 定义中部内容
    var middle = document.createElement("div");
    middle.className = "info-middle";
    middle.style.backgroundColor = 'white';
    middle.innerHTML = content;
    info.appendChild(middle);

    // 定义底部内容
    var bottom = document.createElement("div");
    bottom.className = "info-bottom";
    bottom.style.position = 'relative';
    bottom.style.top = '0px';
    bottom.style.margin = '0 auto';
    var sharp = document.createElement("img");
    sharp.src = "http://webapi.amap.com/images/sharp.png";
    bottom.appendChild(sharp);
    info.appendChild(bottom);
    return info;
}
function openInfo() {
	//逆地理编码
	var lnglatXY=[mapVar.lng, mapVar.lat];//地图上所标点的坐标
	geocoder.getAddress(lnglatXY, function(status, result) {
	    if (status === 'complete' && result.info === 'OK') {
	       //获得了有效的地址信息:
	       //即，result.regeocode.formattedAddress
	       // 地址
		    var address = result.regeocode.formattedAddress;
		    //构建信息窗体中显示的内容
		    var title = '车辆位置和当前时间';
		    var content = [];
		    content.push("地址：" + address);
		    content.push("时间：" + mapVar.time);
		
		    /*content.push("电话：010-64733333");*/
		    content.push("");
		    infoWindow = new AMap.InfoWindow({
		        isCustom: true,  //使用自定义窗体
		        content: createInfoWindow(title, content.join("<br/>")),
		        offset: new AMap.Pixel(16, -45)
		    });
		    infoWindow.open(map, mapVar.marker.getPosition());
	    }else{
	       //获取地址失败
	    }
	});  
}
// 左上角ToolBar控件
//map.plugin(["AMap.ToolBar"], function() {
//	map.addControl(new AMap.ToolBar());
//});
//if(location.href.indexOf('&guide=1')!==-1){
//	map.setStatus({scrollWheel:false})
//}